<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Threadv</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Threadv">
<meta property="og:url" content="http://yoursite.com/page/13/index.html">
<meta property="og:site_name" content="Threadv">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Threadv">
  
    <link rel="alternate" href="/atom.xml" title="Threadv" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Threadv</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-http/http_method" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/14/http/http_method/" class="article-date">
  <time datetime="2018-05-14T03:33:06.000Z" itemprop="datePublished">2018-05-14</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/14/http/http_method/">http_method</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h6 id="http-method"><a href="#http-method" class="headerlink" title="http_method"></a>http_method</h6><ul>
<li>一些常见的HTTP方法</li>
</ul>
<table>
<thead>
<tr>
<th>HTTP方法</th>
<th>描述 </th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>从服务器向客户端发送命名资源</td>
</tr>
<tr>
<td>PUT</td>
<td>将来自客户端的数据存储到一个命名的服务器资源中去</td>
</tr>
<tr>
<td>DELTET</td>
<td>从服务器中删除命名资源</td>
</tr>
<tr>
<td>POST</td>
<td>将客户端数据发送到一个服务器网关应用程序</td>
</tr>
<tr>
<td>HEAD</td>
<td>仅发送命名资源响应中的HTTP首部</td>
</tr>
</tbody>
</table>
<ul>
<li>一些常见的HTTP状态码</li>
</ul>
<table>
<thead>
<tr>
<th>HTTP状态码</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>200</td>
<td>ok,文档正确返回</td>
</tr>
<tr>
<td>302</td>
<td>Redirect(重定向)。到其他地方去获取资源</td>
</tr>
<tr>
<td>404</td>
<td>Not Found(没找到)。无法找到这个资源</td>
</tr>
</tbody>
</table>
<blockquote>
<p> HTTP报文包括以下三个部分。</p>
</blockquote>
<ul>
<li>起始行<br>报文的第一行就是起始行，在请求报文中用来说明要做些什么，在响应报文中说明出现了什么情况。</li>
<li>首部字段<br>起始行后面有零个或多个首部字段。每个首部字段都包含一个名字和一个值，为了便于解析，两者之间用冒号（:）来分隔。首部以一个空行结束。添加一个首部字段和添加新行一样简单。</li>
<li>主体<br>空行之后就是可选的报文主体了，其中包含了所有类型的数据。请求主体中包括了要发送给Web服务器的数据；响应主体中装载了要返回给客户端的数据。起始行和首部都是文本形式且都是结构化的，而主体则不同，主体中可以包含任意的二进制数据（比如图片、视频、音轨、软件程序）。当然，主体中也可以包含文本。</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/14/http/http_method/" data-id="ckcc5iwnu0061q4uwdp4smsji" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Http/">Http</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js内置对象" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/08/js/js内置对象/" class="article-date">
  <time datetime="2018-05-08T10:06:35.000Z" itemprop="datePublished">2018-05-08</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/08/js/js内置对象/">js内置对象Math</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js内置对象Math"><a href="#js内置对象Math" class="headerlink" title="js内置对象Math"></a>js内置对象Math</h5><ul>
<li>Math对象属性</li>
</ul>
<table>
<thead>
<tr>
<th>属性</th>
<th>说明 </th>
</tr>
</thead>
<tbody>
<tr>
<td>Math.E</td>
<td>自然对数的底数，常量e值</td>
</tr>
<tr>
<td>Math.LN10</td>
<td>10的自然对数</td>
</tr>
<tr>
<td>Math.LN2</td>
<td>2的自然对数</td>
</tr>
<tr>
<td>Math.LOG2E</td>
<td>以2为底e的对数</td>
</tr>
<tr>
<td>Math.LOG2E</td>
<td>以10为底e的对数</td>
</tr>
<tr>
<td>Math.PI</td>
<td>π的值</td>
</tr>
<tr>
<td>Math.SQRT1_2</td>
<td>1/2的平方根（2的平方根的倒数）</td>
</tr>
<tr>
<td>Math.SQRT2</td>
<td>2的平方根</td>
</tr>
</tbody>
</table>
<ul>
<li>min()和max()方法</li>
</ul>
<p>找到数组最值，把Math对象作为apply的第一个参数，从而正确的设置this值，任何数组作为第二个参数。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> values = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>];</span><br><span class="line"><span class="keyword">var</span> max = <span class="built_in">Math</span>.max.apply(<span class="built_in">Math</span>,values);</span><br></pre></td></tr></table></figure>
<ul>
<li><p>舍入方法</p>
</li>
<li><ul>
<li>Math.ceil()     向上舍入</li>
</ul>
</li>
<li><ul>
<li>Math.floor()    向下舍入</li>
</ul>
</li>
<li><ul>
<li>Math.round()    标准舍入（四舍五入）</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="built_in">Math</span>.ceil(<span class="number">25.9</span>));     <span class="comment">//26</span></span><br><span class="line">alert(<span class="built_in">Math</span>.ceil(<span class="number">25.5</span>));     <span class="comment">//26</span></span><br><span class="line">alert(<span class="built_in">Math</span>.ceil(<span class="number">25.1</span>));     <span class="comment">//26</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">alert(<span class="built_in">Math</span>.round(<span class="number">25.9</span>));     <span class="comment">//26</span></span><br><span class="line">alert(<span class="built_in">Math</span>.round(<span class="number">25.5</span>));     <span class="comment">//26</span></span><br><span class="line">alert(<span class="built_in">Math</span>.round(<span class="number">25.1</span>));     <span class="comment">//25</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">alert(<span class="built_in">Math</span>.floor(<span class="number">25.9</span>));     <span class="comment">//25</span></span><br><span class="line">alert(<span class="built_in">Math</span>.floor(<span class="number">25.5</span>));     <span class="comment">//25</span></span><br><span class="line">alert(<span class="built_in">Math</span>.floor(<span class="number">25.1</span>));     <span class="comment">//25</span></span><br></pre></td></tr></table></figure>
<ul>
<li>random()方法         </li>
</ul>
<p>值 = Math.floor(Math.random() * 可能的总数 + 第一个可能的值)</p>
<ul>
<li>[Math.floor()方法，Math.random()总返回一个小数值] </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">10</span> +<span class="number">1</span>);     <span class="comment">//1到10 之间的数值</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> num = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">9</span> + <span class="number">2</span>);     <span class="comment">//2到10之间的数值</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">selectFrom</span>(<span class="params">lowerValue,upperValue</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> choices = upperValue - lowerValue + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * choices + lowerValue);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> num = selectFrom(<span class="number">2</span>,<span class="number">10</span>);</span><br><span class="line">alert(num);         <span class="comment">//介于2和10之间（包括2和10）的一个数</span></span><br></pre></td></tr></table></figure>
<ul>
<li>其他方法</li>
</ul>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>Math.abs(num)</td>
<td>返回num的绝对值</td>
</tr>
<tr>
<td>Math.exp(num)</td>
<td>返回Math.E的num次幂</td>
</tr>
<tr>
<td>Math.log(num)</td>
<td>返回num的自然对数</td>
</tr>
<tr>
<td>Math.pow(num,power)</td>
<td>返回num的power次幂</td>
</tr>
<tr>
<td>Math.sqrt(num)</td>
<td>返回num的平方根</td>
</tr>
<tr>
<td>Math.acos(x)</td>
<td>返回x的反余弦值</td>
</tr>
<tr>
<td>Math.asin(x)</td>
<td>返回x的反正弦值</td>
</tr>
<tr>
<td>Math.atan(x)</td>
<td>返回x的反正切值</td>
</tr>
<tr>
<td>Math.atan2(y,x)</td>
<td>返回y/x的反正切值</td>
</tr>
<tr>
<td>Math.cos(x)</td>
<td>返回x的余弦值</td>
</tr>
<tr>
<td>Math.sin(x)</td>
<td>返回x的正弦值</td>
</tr>
<tr>
<td>Math.tan(x)</td>
<td>返回x的正切值</td>
</tr>
</tbody>
</table>
<hr>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/08/js/js内置对象/" data-id="ckcc5iwoi0075q4uwdy0jv7n4" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js面向对象" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/08/js/js面向对象/" class="article-date">
  <time datetime="2018-05-08T10:06:35.000Z" itemprop="datePublished">2018-05-08</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/08/js/js面向对象/">js创建对象</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js创建对象"><a href="#js创建对象" class="headerlink" title="js创建对象"></a>js创建对象</h5><ul>
<li>工厂模式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">createPerson</span>(<span class="params">name, age, job</span>) </span>&#123;</span><br><span class="line">     <span class="keyword">var</span> o = nwe</span><br><span class="line">     <span class="built_in">Object</span>();</span><br><span class="line">     o.name = name;</span><br><span class="line">     o.age = age;</span><br><span class="line">     o.job = job;</span><br><span class="line">     <span class="number">0.</span></span><br><span class="line">     sayName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">         alert(<span class="keyword">this</span>.name)</span><br><span class="line">     &#125;;</span><br><span class="line">     <span class="keyword">return</span> o;</span><br><span class="line"> &#125;</span><br><span class="line"><span class="keyword">var</span> person1 = createPerson(<span class="string">"Nicholas"</span>,<span class="number">29</span>,<span class="string">"Software Engineer"</span>);</span><br><span class="line"><span class="keyword">var</span> person2 = createPerson(<span class="string">"Greg"</span>,<span class="number">27</span>,<span class="string">"Docker"</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>构造函数模式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> person21 = <span class="keyword">new</span> Person(<span class="string">"Nicholas"</span>, <span class="number">29</span>, <span class="string">"Software Engineer"</span>);</span><br><span class="line"><span class="keyword">var</span> person22 = <span class="keyword">new</span> Person(<span class="string">"Greg"</span>, <span class="number">24</span>, <span class="string">"Docker"</span>);</span><br><span class="line"></span><br><span class="line">alert(person21.constructor == Person);      <span class="comment">//true</span></span><br><span class="line">alert(person22.constructor == Person);      <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">alert(person21 <span class="keyword">instanceof</span> <span class="built_in">Object</span>);      <span class="comment">//true</span></span><br><span class="line">alert(person21 <span class="keyword">instanceof</span> Person);      <span class="comment">//true</span></span><br><span class="line">alert(person22 <span class="keyword">instanceof</span> <span class="built_in">Object</span>);      <span class="comment">//true</span></span><br><span class="line">alert(person22 <span class="keyword">instanceof</span> Person);      <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">&lt;!-- <span class="number">2</span>、构造函数--&gt;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">    <span class="keyword">this</span>.age = age;</span><br><span class="line">    <span class="keyword">this</span>.job = job;</span><br><span class="line">    <span class="keyword">this</span>.sayName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="keyword">this</span>.name);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>将构造函数当做函数</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;!--将构造函数当做函数--&gt;</span><br><span class="line"><span class="comment">//当做构造函数使用</span></span><br><span class="line"><span class="keyword">var</span> person = <span class="keyword">new</span> Person(<span class="string">"Nicholas"</span>,<span class="number">24</span>,<span class="string">"Software Engineer"</span>);</span><br><span class="line">person.sayName();       <span class="comment">//"Nicholas"</span></span><br><span class="line"><span class="comment">//当做普通函数调用</span></span><br><span class="line">Person(<span class="string">"Greg"</span>,<span class="number">32</span>,<span class="string">"Docker"</span>);</span><br><span class="line"><span class="built_in">window</span>.sayName();       <span class="comment">//"Greg"</span></span><br><span class="line"><span class="comment">//在另一个对象的作用域中调用</span></span><br><span class="line"><span class="keyword">var</span> 丿= <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br><span class="line">Person .call(o,<span class="string">"Kristen"</span>,<span class="number">32</span>,<span class="string">"Nurse"</span>);</span><br><span class="line">o.sayName();        <span class="comment">//"Kristen"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>[ECMAScript中的函数是对象，每定义一个函数，也就是实例化了一个对象 ] </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name,age,job</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name=name;</span><br><span class="line">    <span class="keyword">this</span>.age=age;</span><br><span class="line">    <span class="keyword">this</span>.job=job;</span><br><span class="line">    <span class="keyword">this</span>.sayName = <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">"alert()this.name"</span>);    <span class="comment">//与声明函数在逻辑上等价</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>原型模式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--</span><br><span class="line">    原型模式--&gt;</span><br><span class="line">    <span class="comment">//创建每个函数都有一个prototype（原型）属性，这个属性是一个指针，指向一个对象，用途是包含可以由特定类型的所有实例共享的属性和方法。</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Person.prototype.name = <span class="string">"Nocjolas"</span>;</span><br><span class="line">    Person.prototype.age = <span class="number">22</span>;</span><br><span class="line">    Person.prototype.job = <span class="string">"Soft ware"</span></span><br><span class="line">    Person.prototype.sayName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="keyword">this</span>.name)</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> person31 = <span class="keyword">new</span> Person();</span><br><span class="line">    person31.sayName();     <span class="comment">//"Nicholas"</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> person32= <span class="keyword">new</span> Person();</span><br><span class="line">    person32.sayName();     <span class="comment">//"Nicholas"</span></span><br><span class="line">    alert(person31.sayName == person32.sayName);    <span class="comment">//trues</span></span><br></pre></td></tr></table></figure>
<ul>
<li>hasOwnProperty()</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> person41 = <span class="keyword">new</span> Person();</span><br><span class="line">   <span class="keyword">var</span> person42 = <span class="keyword">new</span> Person();</span><br><span class="line"></span><br><span class="line">   alert(person41.hasOwnProperty(<span class="string">"name"</span>));     <span class="comment">//false</span></span><br><span class="line">   person41.name=<span class="string">"Greg"</span>;                       <span class="comment">//来自实例</span></span><br><span class="line">   alert(person41.hasOwnProperty(<span class="string">"name"</span>)) ;    <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">   alert(person42.name);                       <span class="comment">//"Nicholas" 来自原型</span></span><br><span class="line">   alert(person42.hasOwnProperty(<span class="string">"name"</span>));     <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">   <span class="keyword">delete</span> person41.name;</span><br><span class="line">   alert(person41.name);                       <span class="comment">//"Nicholas"  来自原型</span></span><br><span class="line">   alert(person41.hasOwnProperty(<span class="string">"name"</span>));     <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   &lt;!--</span><br><span class="line">   原型模式--&gt;</span><br><span class="line">   <span class="comment">//创建每个函数都有一个prototype（原型）属性，这个属性是一个指针，指向一个对象，用途是包含可以由特定类型的所有实例共享的属性和方法。</span></span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   Person.prototype.name = <span class="string">"Nocjolas"</span>;</span><br><span class="line">   Person.prototype.age = <span class="number">22</span>;</span><br><span class="line">   Person.prototype.job = <span class="string">"Soft ware"</span></span><br><span class="line">   Person.prototype.sayName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">       alert(<span class="keyword">this</span>.name)</span><br><span class="line">   &#125;;</span><br></pre></td></tr></table></figure>
<hr>
<p>调用构造函数时会为实例添加一个指向最初原型的<br>[[Prototype]]指针，而把原型修改为另外一个对象就等于切断了构造函数与最初原型之间的联系。<br>请记住：实例中的指针仅指向原型，而不指向构造函数。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> friend = <span class="keyword">new</span> Person();</span><br><span class="line">  Person.prototype=&#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">constructor</span>:Person,</span><br><span class="line">      name:"Greg",</span><br><span class="line">      aage:21,</span><br><span class="line">      job:"Docker",</span><br><span class="line">      sayName:function () &#123;</span><br><span class="line">          alert(<span class="keyword">this</span>.name);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  friend.sayName();       <span class="comment">//error</span></span><br></pre></td></tr></table></figure>
<p>在这个例子中，我们先创建了 Person 的一个实例，然后又重写了其原型对象。然后在调用<br>friend.sayName()时发生了错误，因为 friend 指向的原型中不包含以该名字命名的属性。</p>
<ul>
<li>原型对象的问题</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype =&#123;</span><br><span class="line">    <span class="keyword">constructor</span>:Person,</span><br><span class="line">    name:"jack",</span><br><span class="line">    age:23,</span><br><span class="line">    job:"Engineer",</span><br><span class="line">    friends :["Shlby","Court"],</span><br><span class="line">    sayName:function () &#123;</span><br><span class="line">        alert(<span class="keyword">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> person1 = <span class="keyword">new</span> Person();</span><br><span class="line"><span class="keyword">var</span> person2 = <span class="keyword">new</span> Person();</span><br><span class="line"></span><br><span class="line">person1.friends.push(<span class="string">"van"</span>);</span><br><span class="line"></span><br><span class="line">alert(perosn1.friends);                     <span class="comment">//"Shekby","Court","van"</span></span><br><span class="line">alert(perosn2.friends);                     <span class="comment">//"Shekby","Court","van"</span></span><br><span class="line">alert(perosn1.friends === person2.friends); <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/08/js/js面向对象/" data-id="ckcc5iwoo007hq4uw7y323wus" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js基本包装类型" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/07/js/js基本包装类型/" class="article-date">
  <time datetime="2018-05-07T11:30:46.000Z" itemprop="datePublished">2018-05-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/07/js/js基本包装类型/">js基本包装类型</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js基本包装类型"><a href="#js基本包装类型" class="headerlink" title="js基本包装类型"></a>js基本包装类型</h5><ul>
<li>三种特殊的引用类型：Boolean、Number、String</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> value = <span class="string">"25"</span>;</span><br><span class="line"><span class="keyword">var</span> number = <span class="built_in">Number</span>(value);     <span class="comment">//转型函数</span></span><br><span class="line">alert(<span class="keyword">typeof</span> number);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = <span class="keyword">new</span> <span class="built_in">Number</span>(value);    <span class="comment">//构造函数</span></span><br><span class="line">alert(<span class="keyword">typeof</span> obj);              <span class="comment">//"object"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>Boolean 类型</li>
</ul>
<p>Boolean理性是与布尔类型对应的引用类型。要创建Boolean对象，可以像下面这样调用Boolean构造函数并传入true或false值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> booleanObject = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>
<p>Boolean 类型的实例重写了valueOf方法，返回接班类型值true或false；重写了toString()方法，返回字符串”true”和”false”.</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> falseObject = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">false</span>);   <span class="comment">//false typeOf -&gt; object</span></span><br><span class="line"><span class="keyword">var</span> result = falseObject &amp;&amp; <span class="literal">true</span>;</span><br><span class="line">alert(result);          <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> falseValue = <span class="literal">false</span>;</span><br><span class="line">result = falseValue &amp;&amp; <span class="literal">true</span>;</span><br><span class="line">alert(result);      <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">alert(<span class="keyword">typeof</span> falseObject);      <span class="comment">//object</span></span><br><span class="line">alert(<span class="keyword">typeof</span> falseValue);       <span class="comment">//boolean</span></span><br><span class="line">alert(falseObject <span class="keyword">instanceof</span> <span class="built_in">Boolean</span>);      <span class="comment">//true</span></span><br><span class="line">alert(falseValue <span class="keyword">instanceof</span> <span class="built_in">Boolean</span>);       <span class="comment">//false</span></span><br></pre></td></tr></table></figure>
<ul>
<li>[布尔表达式中所有的对象都会被转换为true ] </li>
<li><p>[理解基本类型的布尔值与Boolean对象之间的区别非常重要–当然，我们建议永远不要使用Boolean对象 ] </p>
</li>
<li><p>Number 类型<br>Number是与数字值对应的引用类型。与Boolean类型一样，Number类型也重写了valueOf()、toLocaleString()和toString()方法。</p>
</li>
</ul>
<ul>
<li><ul>
<li>toString() 返回几进制数值的字符串<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">10</span>;       <span class="comment">//toString（）返回几进制数值的字符串</span></span><br><span class="line"></span><br><span class="line">alert(num.toString());      <span class="comment">//"10"</span></span><br><span class="line">alert(num.toString(<span class="number">2</span>));      <span class="comment">//"1010"</span></span><br><span class="line">alert(num.toString(<span class="number">8</span>));      <span class="comment">//"12"</span></span><br><span class="line">alert(num.toString(<span class="number">10</span>));      <span class="comment">//"10"</span></span><br><span class="line">alert(num.toString(<span class="number">16</span>));      <span class="comment">//"a"</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><ul>
<li>toFixed 按指定的小数位返回数值的字符串</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="number">10.005</span>;</span><br><span class="line">alert(num.toFixed(<span class="number">2</span>));      <span class="comment">//"10.01"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>toExponential() 格式化数值，以指数表示法（e表示法）表示数值的字符串形式。</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="number">10</span> ;</span><br><span class="line">alert(num.toExponential(<span class="number">1</span>));        <span class="comment">//"1.0e+1"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>toPrecision()  返回固定大小（fixed）格式，也可能返回指数（exponential）格式；</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="number">99</span>;</span><br><span class="line">alert(num.toPrecision(<span class="number">1</span>));      <span class="comment">//"1e+2"</span></span><br><span class="line">alert(num.toPrecision(<span class="number">20</span>));     <span class="comment">//"99"</span></span><br><span class="line">alert(num.toPrecision(<span class="number">3</span>));      <span class="comment">//"99.0"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>String 类型  字符串的对象包装类型</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringObject = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">"hello world"</span>);</span><br><span class="line">alert(stringObject.length);         <span class="comment">//"11"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>字符方法  charAt()和charCodeAt()</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringValue = <span class="string">"hello world"</span>;</span><br><span class="line">alert(<span class="string">"stringValue.charAt(1)"</span>);         <span class="comment">//"e" -&gt;字符</span></span><br><span class="line"></span><br><span class="line">alert(stringValue.charCodeAt(<span class="number">1</span>));       <span class="comment">//输出 "101" e字符编码</span></span><br><span class="line"></span><br><span class="line">alert(stringValue[<span class="number">1</span>]);                  <span class="comment">//"e"  字符串特定字符</span></span><br></pre></td></tr></table></figure>
<ul>
<li>-字符串操作方法 concat(),拼接字符串返回新的字符串</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringValue = <span class="string">"hello "</span>;</span><br><span class="line"><span class="keyword">var</span> result = stringValue.concat(<span class="string">"world"</span>);</span><br><span class="line">alert(result);          <span class="comment">//"hello world"</span></span><br><span class="line">alert(stringValue);     <span class="comment">//"hello"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result1 = stringValue.concat(<span class="string">"world"</span>+<span class="string">"!"</span>);</span><br><span class="line">alert(result1);         <span class="comment">//"hello world!"</span></span><br><span class="line">alert(stringValue);     <span class="comment">//"hello"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>slice()、substr()、subString()</li>
</ul>
</li>
</ul>
<p>slice()方法会将传入的负值与字符串长度相加，substr()方法将负的第一个参数加上字符串的长度，而将负的第二个参数转换为0.substring()方法会把所有的负值参数都转换为0。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringValue = <span class="string">"hello world"</span>;</span><br><span class="line">alert(stringValue.slice(<span class="number">-3</span>));               <span class="comment">//"rld"</span></span><br><span class="line">alert(stringValue.substring(<span class="number">-3</span>));           <span class="comment">//"hello world"</span></span><br><span class="line">alert(stringValue.substr(<span class="number">-3</span>));              <span class="comment">//"rld"</span></span><br><span class="line">alert(stringValue.slice(<span class="number">3</span>,<span class="number">-4</span>));             <span class="comment">//"lo w"</span></span><br><span class="line">alert(stringValue.substring(<span class="number">3</span>,<span class="number">-4</span>));         <span class="comment">//"hel"</span></span><br><span class="line">alert(stringValue.substr(<span class="number">3</span>,<span class="number">-4</span>));            <span class="comment">//"" (空字符串)</span></span><br></pre></td></tr></table></figure></p>
<ul>
<li><ul>
<li>字符串位置方法 indexOf() 从头搜索、lastIndexOf()从末尾搜索</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringValue = <span class="string">"hello world"</span>;</span><br><span class="line">alert(stringValue.indexOf(<span class="string">"o"</span>));            <span class="comment">//4</span></span><br><span class="line">alert(stringValue.lastIndexOf(<span class="string">"o"</span>));        <span class="comment">//7</span></span><br><span class="line"></span><br><span class="line">&lt;!--俩个参数--&gt;</span><br><span class="line">alert(stringValue.indexOf(<span class="string">"o"</span>,<span class="number">6</span>));          <span class="comment">//7 从第6位开始向后搜索</span></span><br><span class="line">alert(stringValue.lastIndexOf(<span class="string">"o"</span>,<span class="number">6</span>));      <span class="comment">//4 从第6位开始向前搜索</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>查找字符串所有字符（一个）</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stringValue = <span class="string">"Lorem ipsum dolor sit amet, consectetur adipisicing elit"</span>;</span><br><span class="line"><span class="keyword">var</span> positions = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line"><span class="keyword">var</span> pos = stringValue.indexOf(<span class="string">"e"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(pos &gt; <span class="number">-1</span>)&#123;</span><br><span class="line">    positions.push(pos);</span><br><span class="line">    pos = stringValue.indexOf(<span class="string">"e"</span>,pos+<span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line">alert(positions);           <span class="comment">//"3,24,32,35,52"</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/07/js/js基本包装类型/" data-id="ckcc5iwok007aq4uwbv0ml3uf" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js函数" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/07/js/js函数/" class="article-date">
  <time datetime="2018-05-07T03:30:46.000Z" itemprop="datePublished">2018-05-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/07/js/js函数/">js函数</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js函数"><a href="#js函数" class="headerlink" title="js函数"></a>js函数</h5><ul>
<li>函数的定义</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;--定义方式<span class="number">1</span>--&gt;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1,num2</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1+num2;</span><br><span class="line">&#125;</span><br><span class="line">&lt;--定义方式<span class="number">2</span>--&gt;</span><br><span class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span>(<span class="params">num1,num2</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1+num2;</span><br><span class="line">&#125;</span><br><span class="line">&lt;--定义方式<span class="number">3</span>--&gt;<span class="comment">//不推荐</span></span><br><span class="line"><span class="keyword">var</span> sum = <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">"num1"</span>,<span class="string">"num2"</span>,<span class="string">"return num1+num2"</span>);</span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>函数名仅仅是指向函数的指针，因此函数名与包含对象指针的其他变量没有什么不同。换句话说，一个函数可能会有多个名字:</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1,num2</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1+num2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">alert(sum(<span class="number">10</span>,<span class="number">10</span>));<span class="comment">//20</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> anotherSum = sum;</span><br><span class="line">alert(anoterSum(<span class="number">10</span>,<span class="number">10</span>));<span class="comment">//20</span></span><br><span class="line"></span><br><span class="line">sum = <span class="literal">null</span>;</span><br><span class="line">alert(antoherSum(<span class="number">10</span>,<span class="number">10</span>));<span class="comment">//20</span></span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>函数声明、函数表达式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--可以执行--&gt;</span><br><span class="line">alert(sum(<span class="number">10</span>,<span class="number">10</span>));</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1, num2</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> num1 + num2;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>代码开始执行之前，解析器就已经通过一个名为函数声明提升（function declaration hoisting）的过程，读取并将函数声明添加到执行环境中。对代码求值时，JavaScript引擎在第一遍会声明函数并将它们放到源代码树的顶部。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--异常--&gt;</span><br><span class="line"> alert(sum(<span class="number">10</span>,<span class="number">10</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span> (<span class="params">num1, num2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>函数位于一个初始化语句中，而不是一个函数声明。在执行到函数所在的语句之前，变量sum中不会保存有对函数的引用；而且，由于第一行代码就会导致”unexpected identifier”(意外标识符)错误，实际上也不会执行到下一行。</li>
</ul>
<hr>
<ul>
<li>作为值得函数<br>函数名本身就是变量，所以函数也可以作为值来使用。不仅可以像参数传递一样把一个函数传递给另一个函数，而且可以将一个函数作为另一个函数的结果返回。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callSomeFunction</span>(<span class="params">someFunction, someArgument</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> someFunction(someArgument)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add10</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num + <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result1 = callSomeFunction(add10, <span class="number">10</span>);</span><br><span class="line">alert(result1);     <span class="comment">//20</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getGreeting</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"Hello,"</span> + name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result2 = callSomeFunction(getGreeting,<span class="string">"Nicholas"</span>);</span><br><span class="line">alert(result2);     <span class="comment">//Hello,Nicholas</span></span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>函数内部属性</li>
<li><ul>
<li>arguments.callee</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"><span class="comment">//            return num * factorial(num - 1);</span></span><br><span class="line">            <span class="keyword">return</span> num * <span class="built_in">arguments</span>.callee(num - <span class="number">1</span>); <span class="comment">//解除耦合</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> trueFactorial = factorial;</span><br><span class="line">    factorial = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    alert(trueFactorial(<span class="number">5</span>))     <span class="comment">//120</span></span><br><span class="line">    alert(factorial(<span class="number">5</span>));        <span class="comment">//0</span></span><br></pre></td></tr></table></figure>
<ul>
<li><ul>
<li>this(网页全局作用域调用函数是，this对象引用的就是window)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">window</span>.color = <span class="string">"red"</span>;</span><br><span class="line"><span class="keyword">var</span> o = &#123;<span class="attr">color</span> : <span class="string">"blue"</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>  <span class="title">sayColor</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="keyword">this</span>.color)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">sayColor();     <span class="comment">//"red"</span></span><br><span class="line"></span><br><span class="line">o.sayColor = sayColor;  <span class="comment">//this.color -&gt; o.color</span></span><br><span class="line">o.sayColor();     <span class="comment">//"blue"</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<hr>
<ul>
<li>函数的属性和方法</li>
<li><ul>
<li>length(表示函数希望接收的命名参数的个数)</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayName</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    alert(name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1, num2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayHi</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="string">"Hi!"</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">alert(sayName.length);      <span class="comment">//1</span></span><br><span class="line">alert(sum.length);          <span class="comment">//2</span></span><br><span class="line">alert(sayHi.length);        <span class="comment">//0</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/07/js/js函数/" data-id="ckcc5iwoj0078q4uwf094cp60" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js时间日期" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/05/01/js/js时间日期/" class="article-date">
  <time datetime="2018-05-01T13:30:46.000Z" itemprop="datePublished">2018-05-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/05/01/js/js时间日期/">js时间日期</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js时间日期"><a href="#js时间日期" class="headerlink" title="js时间日期"></a>js时间日期</h5><ul>
<li><p>创建日期</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br></pre></td></tr></table></figure>
</li>
<li><p>在调用 Date 构造函数而不传递参数的情况下，新创建的对象自动获得当前日期和时间。如果想根<br>据特定的日期和时间创建日期对象，必须传入表示该日期的毫秒数（即从 UTC 时间 1970 年 1 月 1 日午<br>夜起至该日期止经过的毫秒数）。为了简化这一计算过程， ECMAScript 提供了两个方法： Date.parse()<br>和 Date.UTC()。<br>其中， Date.parse()方法接收一个表示日期的字符串参数，然后尝试根据这个字符串返回相应日<br>期的毫秒数。 ECMA-262 没有定义 Date.parse()应该支持哪种日期格式，因此这个方法的行为因实现<br>而异，而且通常是因地区而异。将地区设置为美国的浏览器通常都接受下列日期格式：<br> “月/日/年”，如 6/13/2004；<br> “英文月名 日,年”，如 January 12,2004；<br> “英文星期几 英文月名 日 年 时:分:秒 时区”，如 Tue May 25 2004 00:00:00 GMT-0700。<br> ISO 8601 扩展格式 YYYY-MM-DDTHH:mm:ss.sssZ（例如 2004-05-25T00:00:00）。只有兼容<br>ECMAScript 5的实现支持这种格式。<br>例如，要为 2004年 5月 25日创建一个日期对象，可以使用下面的代码：</p>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> someDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="built_in">Date</span>.parse(<span class="string">"May 25, 2004"</span>));</span><br><span class="line">DateTypeExample01.htm</span><br></pre></td></tr></table></figure>
<p>如果传入 Date.parse()方法的字符串不能表示日期，那么它会返回 NaN。实际上，如果直接将表<br>示日期的字符串传递给 Date 构造函数，也会在后台调用 Date.parse()。换句话说，下面的代码与前<br>面的例子是等价的：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> someDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">"May 25, 2004"</span>);</span><br></pre></td></tr></table></figure>
<p>这行代码将会得到与前面相同的日期对象。<br>日期对象及其在不同浏览器中的实现有许多奇怪的行为。其中有一种倾向是将超<br>出范围的值替换成当前的值，以便生成输出。例如，在解析”January 32, 2007”<br>时，有的浏览器会将其解释为”February 1, 2007”。而 Opera 则倾向于插入当前月<br>份的当前日期，返回”January 当前日期， 2007”。也就是说，如果在 2007 年 9 月<br>21 日运行前面的代码，将会得到”January 21, 2007”（都是 21 日）。<br>Date.UTC()方法同样也返回表示日期的毫秒数，但它与 Date.parse()在构建值时使用不同的信<br>息。 Date.UTC()的参数分别是年份、基于 0的月份（一月是 0，二月是 1，以此类推）、月中的哪一天<br>（1 到 31）、小时数（0 到 23）、分钟、秒以及毫秒数。在这些参数中，只有前两个参数（年和月）是必<br>需的。如果没有提供月中的天数，则假设天数为 1；如果省略其他参数，则统统假设为 0。以下是两个<br>使用 Date.UTC()方法的例子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// GMT 时间 2000 年 1 月 1 日午夜零时</span></span><br><span class="line"><span class="keyword">var</span> y2k = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="built_in">Date</span>.UTC(<span class="number">2000</span>, <span class="number">0</span>));</span><br><span class="line"><span class="comment">// GMT 时间 2005 年 5 月 5 日下午 5:55:55</span></span><br><span class="line"><span class="keyword">var</span> allFives = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="built_in">Date</span>.UTC(<span class="number">2005</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">55</span>, <span class="number">55</span>));</span><br><span class="line">DateTypeUTCExample01.htm</span><br></pre></td></tr></table></figure>
<p>这个例子创建了两个日期对象。第一个对象表示 GMT时间 2000年 1月 1日午夜零时，传入的值一<br>个是表示年份的 2000，一个是表示月份的 0（即一月份）。因为其他参数是自动填充的（即月中的天数<br>为 1，其他所有参数均为 0），所以结果就是该月第一天的午夜零时。第二个对象表示 GMT 时间 2005<br>年 5月 5日下午 5:55:55，即使日期和时间中只包含 5，也需要传入不一样的参数：月份必须是 4（因为<br>月份是基于 0的）、小时必须设置为 17（因为小时以 0到 23表示），剩下的参数就很直观了。<br>如同模仿 Date.parse()一样， Date 构造函数也会模仿 Date.UTC()，但有一点明显不同：日期<br>和时间都基于本地时区而非 GMT来创建。不过， Date 构造函数接收的参数仍然与 Date.UTC()相同。<br>因此，如果第一个参数是数值， Date 构造函数就会假设该值是日期中的年份，而第二个参数是月份，<br>以此类推。据此，可以将前面的例子重写如下。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 本地时间 2000 年 1 月 1 日午夜零时</span></span><br><span class="line"><span class="keyword">var</span> y2k = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="number">2000</span>, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 本地时间 2005 年 5 月 5 日下午 5:55:55</span></span><br><span class="line"><span class="keyword">var</span> allFives = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="number">2005</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">55</span>, <span class="number">55</span>);</span><br><span class="line">DateTypeConstructorExample01.htm</span><br></pre></td></tr></table></figure></p>
<p>以上代码创建了与前面例子中相同的两个日期对象，只不过这次的日期都是基于系统设置的本地时<br>区创建的。<br>ECMAScript 5 添加了 Data.now()方法，返回表示调用这个方法时的日期和时间的毫秒数。这个方<br>法简化了使用 Data 对象分析代码的工作。例如：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//取得开始时间</span></span><br><span class="line"><span class="keyword">var</span> start = <span class="built_in">Date</span>.now();</span><br><span class="line"><span class="comment">//调用函数</span></span><br><span class="line">doSomething();</span><br><span class="line"><span class="comment">//取得停止时间</span></span><br><span class="line"><span class="keyword">var</span> stop = <span class="built_in">Date</span>.now(),</span><br><span class="line">result = stop – start;</span><br><span class="line">支持 Data.now()方法的浏览器包括 IE9+、 Firefox <span class="number">3</span>+、 Safari <span class="number">3</span>+、 Opera <span class="number">10.5</span> 和 Chrome。在不支</span><br><span class="line">持它的浏览器中，使用+操作符把 Data 对象转换成字符串，也可以达到同样的目的。</span><br><span class="line"><span class="comment">//取得开始时间</span></span><br><span class="line"><span class="keyword">var</span> start = +<span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line"><span class="comment">//调用函数</span></span><br><span class="line">doSomething();</span><br><span class="line"><span class="comment">//取得停止时间</span></span><br><span class="line"><span class="keyword">var</span> stop = +<span class="keyword">new</span> <span class="built_in">Date</span>(),</span><br><span class="line">result = stop - start;</span><br></pre></td></tr></table></figure></p>
<hr>
<h5 id="日期的格式化方法"><a href="#日期的格式化方法" class="headerlink" title="日期的格式化方法"></a>日期的格式化方法</h5><p>Date 类型还有一些专门用于将日期格式化为字符串的方法，这些方法如下。<br> toDateString()——以特定于实现的格式显示星期几、月、日和年；<br> toTimeString()——以特定于实现的格式显示时、分、秒和时区；<br> toLocaleDateString()——以特定于地区的格式显示星期几、月、日和年；<br> toLocaleTimeString()——以特定于实现的格式显示时、分、秒；<br> toUTCString()——以特定于实现的格式完整的 UTC 日期。<br>与 toLocaleString()和 toString()方法一样，以上这些字符串格式方法的输出也是因浏览器<br>而异的，因此没有哪一个方法能够用来在用户界面中显示一致的日期信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">除了前面介绍的方法之外，还有一个名叫 toGMTString()的方法，这是一个与</span><br><span class="line">toUTCString()等价的方法，其存在目的在于确保向后兼容。不过， ECMAScript 推</span><br><span class="line">荐现在编写的代码一律使用 toUTCString()方法。</span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>日期/时间组件方法<br>到目前为止，剩下还未介绍的 Date 类型的方法（如下表所示），都是直接取得和设置日期值中特<br>定部分的方法了。需要注意的是， UTC 日期指的是在没有时区偏差的情况下（将日期转换为 GMT 时间）<br>的日期值。</li>
</ul>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>getTime()</td>
<td>返回表示日期的毫秒数；与valueOf()方法返回的值相同</td>
</tr>
<tr>
<td>setTime(毫秒)</td>
<td>以毫秒数设置日期，会改变整个日期</td>
</tr>
<tr>
<td>getFullYear()</td>
<td>取得4位数的年份（如2007而非仅07）</td>
</tr>
<tr>
<td>getUTCFullYear()</td>
<td>返回UTC日期的4位数年份</td>
</tr>
<tr>
<td>setFullYear(年)</td>
<td>设置日期的年份。传入的年份值必须是4位数字（如2007而非仅07）</td>
</tr>
<tr>
<td>setUTCFullYear(年)</td>
<td>设置UTC日期的年份。传入的年份值必须是4位数字（如2007而非仅07）</td>
</tr>
<tr>
<td>getMonth()</td>
<td>返回日期中的月份，其中0表示一月， 11表示十二月</td>
</tr>
<tr>
<td>getUTCMonth()</td>
<td>返回UTC日期中的月份，其中0表示一月， 11表示十二月</td>
</tr>
<tr>
<td>setMonth(月)</td>
<td>设置日期的月份。传入的月份值必须大于0，超过11则增加年份</td>
</tr>
<tr>
<td>setUTCMonth(月)</td>
<td>设置UTC日期的月份。传入的月份值必须大于0，超过11则增加年份</td>
</tr>
<tr>
<td>getDate()</td>
<td>返回日期月份中的天数（1到31）</td>
</tr>
<tr>
<td>getUTCDate()</td>
<td>返回UTC日期月份中的天数（1到31）</td>
</tr>
<tr>
<td>setDate(日)</td>
<td>设置日期月份中的天数。如果传入的值超过了该月中应有的天数，则增加月份</td>
</tr>
<tr>
<td>setUTCDate(日)</td>
<td>设置UTC日期月份中的天数。如果传入的值超过了该月中应有的天数，则增加月份</td>
</tr>
<tr>
<td>getDay()</td>
<td>返回日期中星期的星期几（其中0表示星期日， 6表示星期六）</td>
</tr>
<tr>
<td>getUTCDay()</td>
<td>返回UTC日期中星期的星期几（其中0表示星期日， 6表示星期六）</td>
</tr>
<tr>
<td>getHours()</td>
<td>返回日期中的小时数（0到23）</td>
</tr>
<tr>
<td>getUTCHours()</td>
<td>返回UTC日期中的小时数（0到23）</td>
</tr>
<tr>
<td>setHours(时)</td>
<td>设置日期中的小时数。传入的值超过了23则增加月份中的天数</td>
</tr>
<tr>
<td>setUTCHours(时)</td>
<td>设置UTC日期中的小时数。传入的值超过了23则增加月份中的天数</td>
</tr>
<tr>
<td>getMinutes()</td>
<td>返回日期中的分钟数（0到59）</td>
</tr>
<tr>
<td>getUTCMinutes()</td>
<td>返回UTC日期中的分钟数（0到59）</td>
</tr>
<tr>
<td>setMinutes(分)</td>
<td>设置日期中的分钟数。传入的值超过59则增加小时数</td>
</tr>
<tr>
<td>setUTCMinutes(分)</td>
<td>设置UTC日期中的分钟数。传入的值超过59则增加小时数</td>
</tr>
<tr>
<td>getSeconds()</td>
<td>返回日期中的秒数（0到59）</td>
</tr>
<tr>
<td>getUTCSeconds()</td>
<td>返回UTC日期中的秒数（0到59）</td>
</tr>
<tr>
<td>setSeconds(秒)</td>
<td>设置日期中的秒数。传入的值超过了59会增加分钟数</td>
</tr>
<tr>
<td>setUTCSeconds(秒)</td>
<td>设置UTC日期中的秒数。传入的值超过了59会增加分钟数</td>
</tr>
<tr>
<td>getMilliseconds()</td>
<td>返回日期中的毫秒数</td>
</tr>
<tr>
<td>getUTCMilliseconds()</td>
<td>返回UTC日期中的毫秒数</td>
</tr>
<tr>
<td>setMilliseconds(毫秒)</td>
<td>设置日期中的毫秒数</td>
</tr>
<tr>
<td>setUTCMilliseconds(毫秒)</td>
<td>设置UTC日期中的毫秒数</td>
</tr>
<tr>
<td>getTimezoneOffset()</td>
<td>返回本地时间与UTC时间相差的分钟数。例如，美国东部标准时间返回300。在某地进入夏令时的情况下，这个值会有所变化</td>
</tr>
</tbody>
</table>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/05/01/js/js时间日期/" data-id="ckcc5iwon007fq4uwuvz7n9hk" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js位置方法" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/04/29/js/js位置方法/" class="article-date">
  <time datetime="2018-04-29T11:10:35.000Z" itemprop="datePublished">2018-04-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/04/29/js/js位置方法/">js位置方法</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js位置方法"><a href="#js位置方法" class="headerlink" title="js位置方法"></a>js位置方法</h5><p>ECMAScript 5 为数组实例添加了两个位置方法： indexOf()和 lastIndexOf()。这两个方法都接收<br>两个参数：要查找的项和（可选的）表示查找起点位置的索引。其中， indexOf()方法从数组的开头（位<br>置 0）开始向后查找， lastIndexOf()方法则从数组的末尾开始向前查找。<br>这两个方法都返回要查找的项在数组中的位置，或者在没找到的情况下返回-1。在比较第一个参数<br>与数组中的每一项时，会使用全等操作符；也就是说，要求查找的项必须严格相等（就像使用===一样）。</p>
<hr>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> numbers = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>];</span><br><span class="line">        alert(numbers.indexOf(<span class="number">4</span>));      <span class="comment">// 3</span></span><br><span class="line">        alert(numbers.lastIndexOf(<span class="number">4</span>));  <span class="comment">// 5</span></span><br><span class="line">        alert(numbers.indexOf(<span class="number">4</span>,<span class="number">4</span>));    <span class="comment">// 5</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">var</span> person = &#123;<span class="attr">name</span> : <span class="string">"jack"</span>&#125;;</span><br><span class="line">        <span class="keyword">var</span> people = [&#123;<span class="attr">name</span> : <span class="string">"jack"</span>&#125;];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">var</span> morePeople = [person];</span><br><span class="line"></span><br><span class="line">        alert(people.indexOf(person));      <span class="comment">//-1</span></span><br><span class="line">        alert(morePeople.indexOf(person));  <span class="comment">//0</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/04/29/js/js位置方法/" data-id="ckcc5iwog0073q4uwskyxp3bw" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-js/js数组排序" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/04/28/js/js数组排序/" class="article-date">
  <time datetime="2018-04-28T09:32:26.000Z" itemprop="datePublished">2018-04-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/04/28/js/js数组排序/">js数组排序</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="js数组排序"><a href="#js数组排序" class="headerlink" title="js数组排序"></a>js数组排序</h5><hr>
<h2 id="reverse-和sort-方法的返回值都是经过排序之后的数组"><a href="#reverse-和sort-方法的返回值都是经过排序之后的数组" class="headerlink" title="reverse()和sort()方法的返回值都是经过排序之后的数组"></a>reverse()和sort()方法的返回值都是经过排序之后的数组</h2><ul>
<li>reverse</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var values = [0,1,5,10,15];</span><br><span class="line">values.reverse();</span><br><span class="line">alert(values)  //15,10,,5,1,0</span><br></pre></td></tr></table></figure>
<ul>
<li>sort</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var values = [0,1,5,10,15];</span><br><span class="line">values.sort();</span><br><span class="line">alert(values)  //0,1,10,15,5</span><br></pre></td></tr></table></figure>
<ul>
<li>添加比较函数-升序</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var values = [0,1,5,10,15];</span><br><span class="line">values.sort(compare);</span><br><span class="line">alert(values)  //0,1,5,10,15</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">function compare(v1, v2) &#123;</span><br><span class="line">       if (v1 <span class="tag">&lt; <span class="attr">v2</span>) &#123;</span></span><br><span class="line"><span class="tag">           <span class="attr">return</span> <span class="attr">-1</span>;</span></span><br><span class="line"><span class="tag">       &#125; <span class="attr">else</span> <span class="attr">if</span> (<span class="attr">v1</span> &gt;</span> v2) &#123;</span><br><span class="line">           return 1;</span><br><span class="line">       &#125; else &#123;</span><br><span class="line">           return 0;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>降序</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var values = [0,1,5,10,15];</span><br><span class="line">values.sort(compare);</span><br><span class="line">alert(values)  //15,10,,5,1,0</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">function compare(v1, v2) &#123;</span><br><span class="line">       if (v1 <span class="tag">&lt; <span class="attr">v2</span>) &#123;</span></span><br><span class="line"><span class="tag">           <span class="attr">return</span> <span class="attr">1</span>;</span></span><br><span class="line"><span class="tag">       &#125; <span class="attr">else</span> <span class="attr">if</span> (<span class="attr">v1</span> &gt;</span> v2) &#123;</span><br><span class="line">           return -1;</span><br><span class="line">       &#125; else &#123;</span><br><span class="line">           return 0;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/04/28/js/js数组排序/" data-id="ckcc5iwol007cq4uwdu9ln05m" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-java/java_定时任务" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/04/28/java/java_定时任务/" class="article-date">
  <time datetime="2018-04-28T07:02:19.000Z" itemprop="datePublished">2018-04-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/04/28/java/java_定时任务/">定时任务</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h6 id="定时任务"><a href="#定时任务" class="headerlink" title="定时任务"></a>定时任务</h6><ul>
<li>线程等待（不建议使用，任务复杂时存在内存泄露风险<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestThreadWait</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Thread myThread = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"TestThreadWait is called!"</span>);</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="comment">// 使用线程休眠来实现周期执行</span></span><br><span class="line">                        Thread.sleep(<span class="number">1000</span> * <span class="number">3</span>); </span><br><span class="line">                    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                        e.printStackTrace();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        myThread.start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ul>
<li>Timer**<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTimer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        TimerTask task = <span class="keyword">new</span> TimerTask() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"TimerTask is called!"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        Timer timer = <span class="keyword">new</span> Timer();</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         * schedule 和 scheduleAtFixedRate 区别：</span></span><br><span class="line"><span class="comment">         *  可将schedule理解为scheduleAtFixedDelay，</span></span><br><span class="line"><span class="comment">         *  两者主要区别在于delay和rate</span></span><br><span class="line"><span class="comment">         *  1、schedule，如果第一次执行被延时（delay），</span></span><br><span class="line"><span class="comment">         *      随后的任务执行时间将以上一次任务实际执行完成的时间为准</span></span><br><span class="line"><span class="comment">         *  2、scheduleAtFixedRate，如果第一次执行被延时（delay），</span></span><br><span class="line"><span class="comment">         *      随后的任务执行时间将以上一次任务开始执行的时间为准（需考虑同步）</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">         *  参数：1、任务体    2、延时时间（可以指定执行日期）3、任务执行间隔时间</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="comment">// timer.schedule(task, 0, 1000 * 3);</span></span><br><span class="line">        timer.scheduleAtFixedRate(task, <span class="number">0</span>, <span class="number">1000</span> * <span class="number">3</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ul>
<li>ScheduledExecutorService**<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestScheduledExecutorService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Runnable runnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"ScheduledExecutorService Task is called!"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();</span><br><span class="line">        <span class="comment">// 参数：1、任务体 2、首次执行的延时时间 </span></span><br><span class="line">        <span class="comment">//      3、任务执行间隔 4、间隔时间单位</span></span><br><span class="line">        service.scheduleAtFixedRate(runnable, <span class="number">0</span>, <span class="number">3</span>, TimeUnit.SECONDS);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ul>
<li>Quartz**<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestQuartz</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();</span><br><span class="line">            scheduler.start();</span><br><span class="line"></span><br><span class="line">            JobDetail job = newJob(Job.class)</span><br><span class="line">                    .withIdentity(<span class="string">"job"</span>, <span class="string">"group"</span>).build();</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 休眠时长可指定时间单位，此处使用秒作为单位（withIntervalInSeconds）</span></span><br><span class="line">            Trigger trigger = newTrigger()</span><br><span class="line">                    .withIdentity(<span class="string">"trigger"</span>, <span class="string">"group"</span>).startNow()</span><br><span class="line">                    .withSchedule(simpleSchedule().withIntervalInSeconds(<span class="number">3</span>).repeatForever())</span><br><span class="line">                    .build();</span><br><span class="line"></span><br><span class="line">            scheduler.scheduleJob(job, trigger);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// scheduler.shutdown();</span></span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span> (SchedulerException se) &#123;</span><br><span class="line">            se.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Job</span> <span class="keyword">implements</span> <span class="title">org</span>.<span class="title">quartz</span>.<span class="title">Job</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">(JobExecutionContext context)</span> <span class="keyword">throws</span> JobExecutionException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Quartz task is called!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/04/28/java/java_定时任务/" data-id="ckcc5iwoe006xq4uwc2ey8h4x" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-json2Map" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/04/18/json2Map/" class="article-date">
  <time datetime="2018-04-18T09:02:36.000Z" itemprop="datePublished">2018-04-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/04/18/json2Map/">json2Map</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="json2Map"><a href="#json2Map" class="headerlink" title="json2Map"></a>json2Map</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    String str = <span class="string">"&#123;\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"&#125;"</span>;  </span><br><span class="line">    <span class="comment">//第一种方式  </span></span><br><span class="line">    Map maps = (Map)JSON.parse(str);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSON类来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object map : maps.entrySet())&#123;  </span><br><span class="line">        System.out.println(((Map.Entry)map).getKey()+<span class="string">"     "</span> + ((Map.Entry)map).getValue());  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//第二种方式  </span></span><br><span class="line">    Map mapTypes = JSON.parseObject(str);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSON类的parseObject来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object obj : mapTypes.keySet())&#123;  </span><br><span class="line">        System.out.println(<span class="string">"key为："</span>+obj+<span class="string">"值为："</span>+mapTypes.get(obj));  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//第三种方式  </span></span><br><span class="line">    Map mapType = JSON.parseObject(str,Map.class);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSON类,指定解析类型，来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object obj : mapType.keySet())&#123;  </span><br><span class="line">        System.out.println(<span class="string">"key为："</span>+obj+<span class="string">"值为："</span>+mapType.get(obj));  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//第四种方式  </span></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * JSONObject是Map接口的一个实现类 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    Map json = (Map) JSONObject.parse(str);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSONObject类的parse方法来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object map : json.entrySet())&#123;  </span><br><span class="line">        System.out.println(((Map.Entry)map).getKey()+<span class="string">"  "</span>+((Map.Entry)map).getValue());  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//第五种方式  </span></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * JSONObject是Map接口的一个实现类 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    JSONObject jsonObject = JSONObject.parseObject(str);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSONObject的parseObject方法来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object map : json.entrySet())&#123;  </span><br><span class="line">        System.out.println(((Map.Entry)map).getKey()+<span class="string">"  "</span>+((Map.Entry)map).getValue());  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//第六种方式  </span></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * JSONObject是Map接口的一个实现类 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    Map mapObj = JSONObject.parseObject(str,Map.class);  </span><br><span class="line">    System.out.println(<span class="string">"这个是用JSONObject的parseObject方法并执行返回类型来解析JSON字符串!!!"</span>);  </span><br><span class="line">    <span class="keyword">for</span> (Object map: json.entrySet())&#123;  </span><br><span class="line">        System.out.println(((Map.Entry)map).getKey()+<span class="string">"  "</span>+((Map.Entry)map).getValue());  </span><br><span class="line">    &#125;  </span><br><span class="line">    String strArr = <span class="string">"&#123;&#123;\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"&#125;,"</span> +  </span><br><span class="line">            <span class="string">"&#123;\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"&#125;&#125;"</span>;  </span><br><span class="line">   <span class="comment">// JSONArray.parse()  </span></span><br><span class="line">    System.out.println(json);  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/04/18/json2Map/" data-id="ckcc5iwkj0026q4uwxeuowriw" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/12/">&laquo; zurück</a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/11/">11</a><a class="page-number" href="/page/12/">12</a><span class="page-number current">13</span><a class="page-number" href="/page/14/">14</a><a class="page-number" href="/page/15/">15</a><a class="extend next" rel="next" href="/page/14/">weiter &raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Design-Patterns/">Design Patterns</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Html/">Html</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Http/">Http</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sort/">Sort</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/algorithm/">algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ali-weixin-pay/">ali/weixin pay</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/elasticsearch/">elasticsearch</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/idea/">idea</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jvm/">jvm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logstash/">logstash</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pytest/">pytest</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/util/">util</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vue/">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/wechat/">wechat</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/我的世界/">我的世界</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/消息队列/">消息队列</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/线程/">线程</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Design-Patterns/" style="font-size: 16.36px;">Design Patterns</a> <a href="/tags/Git/" style="font-size: 10px;">Git</a> <a href="/tags/Html/" style="font-size: 10px;">Html</a> <a href="/tags/Http/" style="font-size: 12.73px;">Http</a> <a href="/tags/JavaScript/" style="font-size: 15.45px;">JavaScript</a> <a href="/tags/Sort/" style="font-size: 18.18px;">Sort</a> <a href="/tags/algorithm/" style="font-size: 11.82px;">algorithm</a> <a href="/tags/ali-weixin-pay/" style="font-size: 13.64px;">ali/weixin pay</a> <a href="/tags/docker/" style="font-size: 17.27px;">docker</a> <a href="/tags/elasticsearch/" style="font-size: 10px;">elasticsearch</a> <a href="/tags/go/" style="font-size: 11.82px;">go</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/idea/" style="font-size: 10px;">idea</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/jvm/" style="font-size: 11.82px;">jvm</a> <a href="/tags/linux/" style="font-size: 18.18px;">linux</a> <a href="/tags/logstash/" style="font-size: 12.73px;">logstash</a> <a href="/tags/mysql/" style="font-size: 14.55px;">mysql</a> <a href="/tags/php/" style="font-size: 11.82px;">php</a> <a href="/tags/pytest/" style="font-size: 10px;">pytest</a> <a href="/tags/python/" style="font-size: 13.64px;">python</a> <a href="/tags/springboot/" style="font-size: 17.27px;">springboot</a> <a href="/tags/util/" style="font-size: 19.09px;">util</a> <a href="/tags/vue/" style="font-size: 10px;">vue</a> <a href="/tags/wechat/" style="font-size: 10.91px;">wechat</a> <a href="/tags/我的世界/" style="font-size: 10px;">我的世界</a> <a href="/tags/消息队列/" style="font-size: 10.91px;">消息队列</a> <a href="/tags/线程/" style="font-size: 11.82px;">线程</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">February 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">January 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">December 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">September 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">June 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">April 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">January 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">December 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/05/17/linux/linux_shell/">linux—shell</a>
          </li>
        
          <li>
            <a href="/2020/05/16/linux/linux_awk_sed/">linux-awk\sed 示例</a>
          </li>
        
          <li>
            <a href="/2020/04/11/linux/linux_bash/">linux—bash</a>
          </li>
        
          <li>
            <a href="/2020/03/29/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/2020/03/16/go基础/">go基础知识</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 vchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>